﻿// Copyright (c) 2010 http://xbeeservice.codeplex.com
// This source is subject to the Microsoft Public License.
// See http://www.opensource.org/licenses/ms-pl.html
// All other rights reserved.

using System;
using System.Collections.Generic;
using System.Text;

namespace SoCGadget.Robotics
{
    /// <summary>
    /// Subclass of the W3C.Soap.Fault class to help in creating faults for Dss services.
    /// </summary>
    public class SoapFault : W3C.Soap.Fault
    {
        private SoapFault()
        {
            this.Code = new W3C.Soap.FaultCode();
            this.Code.Value = W3C.Soap.FaultCodes.Receiver;
            this.Detail = new W3C.Soap.Detail();
            this.Node = "Node";
            this.Role = "Role";
        }

        public SoapFault( string message ) :
            this()
        {
            W3C.Soap.ReasonText[] reasons = 
                {
                    new W3C.Soap.ReasonText(),
                };
            reasons[ 0 ].Value = message;
            this.Reason = reasons;
        }

        public SoapFault( string message, Exception ex ) :
            this()
        {
            W3C.Soap.ReasonText[] reasons = 
                {
                    new W3C.Soap.ReasonText(),
                    new W3C.Soap.ReasonText(),
                };
            reasons[ 0 ].Value = message;
            reasons[ 1 ].Value = ex.ToString();
            this.Reason = reasons;
        }

        public SoapFault( Exception ex ) :
            this()
        {
            W3C.Soap.ReasonText[] reasons = 
                {
                    new W3C.Soap.ReasonText(),
                };
            reasons[ 0 ].Value = ex.ToString();
            this.Reason = reasons;
        }

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();

            foreach ( W3C.Soap.ReasonText reason in this.Reason )
                sb.AppendFormat( "{0}; ", reason.Value );

            return sb.ToString();
        }
    }
}
